﻿using CuaHangLego.Bussiness.Parser;
using CuaHangLego.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CuaHangLego.DataAccess
{
    public class CTHDBanDA : DABase
    {
        public static CTHDBanDA Instance = new CTHDBanDA();

        public int InsertToHoaDon(int hd, CTHD item)
        {
            SqlCommand sql = new SqlCommand();
            sql.CommandText = string.Format("INSERT INTO CTHDBAN ({0},{1},{2}) VALUES({3},{4},{5});",
                   "HoaDonID", "SanPhamID", "SoLuong",
                   hd, item.SanPham.MaSo, item.SoLuong);
            this.OpenDB();
            int rs = ExecuteQuery(sql);
            this.CloseDB();
            return rs;
        }

        public bool UpdateFromHD(int hd, CTHD item)
        {
            SqlCommand sql = new SqlCommand();
            sql.CommandText = string.Format("UPDATE CTHDBAN SET {0}={2},{1}={3} WHERE {4}={5} AND {6}={7};",
                "SanPhamID", "SoLuong",
                item.SanPham.MaSo, item.SoLuong,
                "HoaDonID", hd,
                "SanPhamID", item.SanPham.MaSo);
            OpenDB();
            int rs = int.Parse(ExecuteQuery(sql).ToString());
            CloseDB();
            return rs > 0;
        }

        public bool DeleteFromHD(int hd, CTHD item)
        {
            SqlCommand sql = new SqlCommand();
            sql.CommandText = string.Format("DELETE FROM CTHDBAN WHERE {0}={1} AND {2}={3}",
                "HoaDonID", hd,
                "SanPhamID", item.SanPham.MaSo);
            OpenDB();
            int rs = int.Parse(ExecuteQuery(sql).ToString());
            CloseDB();
            return rs > 0;
        }

        public IList<CTHD> GetAllFromHD(int hd)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = string.Format("SELECT * FROM CTHDBAN WHERE {0}={1}", "HoaDonID", hd);
            cmd.CommandType = System.Data.CommandType.Text;
            OpenDB();
            SqlDataReader reader = ExecuteQueryReader(cmd);
            IList<CTHD> list = new List<CTHD>();
            while (reader.Read())
            {
                list.Add(CTHDParser.Parse(reader));
            }
            CloseDB();
            return list;
        }

        public IList<CTHD> GetAll()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = string.Format("SELECT * FROM CTHDBAN as A, HOADONBAN as B WHERE A.HoaDonID=B.ID AND B.DaThanhToan=1");
            cmd.CommandType = System.Data.CommandType.Text;
            OpenDB();
            SqlDataReader reader = ExecuteQueryReader(cmd);
            IList<CTHD> list = new List<CTHD>();
            while (reader.Read())
            {
                list.Add(CTHDParser.Parse(reader));
            }
            CloseDB();
            return list;
        }
    }
}
